Skip to main content

Java Interface vs. Abstract Class

Banner java icon

๐Ÿ† The Ultimate Java Showdown: Abstract Class vs. Interfaceโ€‹

Abstract classes and interfaces are the MVPs of Java APIs! But how do they differ? Let's break it down in a fun way. Grab your popcorn, folks! ๐Ÿฟ

1๏ธโƒฃ Abstract Class - The "Almost There" Blueprint ๐ŸŽญโ€‹

An abstract class is like an incomplete superheroโ€”strong, but still missing a piece of the puzzle. Itโ€™s defined using the abstract keyword and may or may not contain abstract methods.

Example Time! ๐ŸŽฌโ€‹

public abstract class TestAbstractClass {

public abstract void abstractMethod(); // Unfinished business ๐Ÿง
public void normalMethod() { ... method body ... } // All good here! โœ…
}

โš ๏ธ Important Rule: If a class has an abstract method, the whole class must be declared as abstract! Otherwise, Java will give you a stern look. ๐Ÿ‘€

๐Ÿค” So, why use something you can't even instantiate?

Because it's meant to be extended! Think of it like a foundation for your dream house. ๐Ÿก

class ChildClass extends TestAbstractClass {

@Override
public void abstractMethod() {
// Completing the unfinished business! ๐ŸŽ‰
}
}

2๏ธโƒฃ Interface - The "Contract" You Can't Ignore ๐Ÿ“œโ€‹

An interface is like a rulebook ๐Ÿ“–โ€”every class that signs up must follow all the rules. All methods in an interface are inherently public and abstract (unless you're Java 8+โ€”more on that later! ๐Ÿ˜‰).

Example ๐Ÿš€โ€‹

public interface TestInterface {
void implementMe(); // A promise you HAVE to keep! ๐Ÿค
}

And if a class wants to be part of this elite group? It must implement the methods!

public class TestMain implements TestInterface {
@Override
public void implementMe() {
// Following the contract! โœ…
}
}

3๏ธโƒฃ Abstract Class Implementing an Interface ๐Ÿคฏโ€‹

The only time you don't need to override an interface method is if you declare the class abstract. Then, it just sits there, waiting for someone else to complete the work. ๐Ÿ› ๏ธ

public abstract class AbstractClass implements TestInterface {
// No need to override implementMe() ๐Ÿ˜Œ
}

But when a normal class extends it? No more free rides! ๐ŸŽข

public class ChildClass extends AbstractClass {
@Override
public void implementMe() {
// Finally doing the work! ๐Ÿ”ฅ
}
}

4๏ธโƒฃ Abstract Class vs. Interface: The Ultimate Face-Off! โšกโ€‹

FeatureAbstract Class ๐Ÿ›๏ธInterface ๐Ÿ“œ
MethodsCan have both abstract & non-abstract methodsOnly abstract methods (until Java 8)
Access ModifiersCan be public, protected, privateOnly public
Static MethodsAllowed โœ…Not allowed (except default methods in Java 8)
Multiple InheritanceโŒ Only one superclassโœ… Can implement multiple interfaces
InstantiationโŒ Nope, can't do itโŒ Nope, still can't

5๏ธโƒฃ When to Use What? ๐Ÿคทโ€โ™‚๏ธโ€‹

๐Ÿ›  Use Abstract Classes When...โ€‹

  • You need to add partial behavior
  • You want to reuse code (like HttpServlet in Java EE ๐ŸŒ)
  • You need shared state (like instance variables)

Exampleโ€‹

public abstract class HttpServlet {
public void init() {
System.out.println("Initialization done!");
}
public abstract void service(); // Must be implemented!
}

๐Ÿ“œ Use Interfaces When...โ€‹

  • You need pure abstraction (just contracts, no implementation)
  • You want multiple inheritance-like behavior
  • You need to define different behaviors in different places

Example1โ€‹

public interface Map<K, V> {
void put(K key, V value);
V get(K key);
}

6๏ธโƒฃ Java 8: Default Methods - The Game Changer! ๐ŸŽฎโ€‹

Since Java 8, interfaces can have default methods! This means they can provide some implementation, making them almost like abstract classes. ๐Ÿ˜ฒ

public interface Moveable {
default void move(){
System.out.println("I am moving");
}
}

And now, any class implementing Moveable can just use the default method OR override it!

public class Animal implements Moveable {
public static void main(String[] args){
Animal tiger = new Animal();
tiger.move(); // Output: I am moving ๐Ÿƒโ€โ™‚๏ธ
}
}

Or, if you prefer your own twist:

public class Animal implements Moveable {
public void move(){
System.out.println("I am running");
}
public static void main(String[] args){
Animal tiger = new Animal();
tiger.move(); // Output: I am running ๐Ÿƒโ€โ™‚๏ธ๐Ÿ’จ
}
}

7๏ธโƒฃ Java 8's Impact on the Abstract Class vs. Interface Battle โš”๏ธโ€‹

With default methods, interfaces have gotten a serious upgrade. The only major difference left is that Java still doesnโ€™t allow multiple inheritance of classes, but does allow multiple interface implementations.

๐ŸŽฏ Key Takeaway: Interfaces now do almost everything an abstract class can! So, always think about your design before choosing one over the other.


๐ŸŽ‰ Conclusion: Who Wins? ๐Ÿค”โ€‹

Both abstract classes and interfaces have their place in Java. It all depends on what you need: โœ… Need to define behavior? Use an abstract class! โœ… Need a strict contract with multiple implementations? Use an interface!

๐Ÿ’ก Choose wisely, and happy coding! ๐Ÿš€๐Ÿ”ฅ